﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using iHub.Properties;
using System.Collections;
using Excel = Microsoft.Office.Interop.Excel;

namespace iHub
{
    public partial class main : Form
    {
        Image img;
        Image back;

        //For Employees
        Boolean currentStatus = true;
        AssignmentManager assm = new AssignmentManager();
        EmployeeManager en = new EmployeeManager();
        AccountManager acctm = new AccountManager();
        PayrollManager paym = new PayrollManager();

        //For Payroll
        DataTable dt = new DataTable();

        DataTable sana = new DataTable();
        DataSet sana1 = new DataSet();

        public static main form = null;
        //datarow na public na static
        String userType = "";

        //For Employees

        //For Payroll
        public static Account nacct = new Account();
        String user;
        String pass;
        String date;
        String time;
        Boolean usertype = false;
        String empname;
        int advances;
        DataTable cv = new DataTable();
        DataTable plist = new DataTable();
        DataTable bir = new DataTable();
        DateTime cvdate = new DateTime();
        DateTime listbegin = new DateTime();
        DateTime listend = new DateTime();

        public main(Account a, String d, String t)
        {
            InitializeComponent();
            this.FormClosing += new FormClosingEventHandler(Start_Closing);

            back = Resources.menuButton;
            img = Resources.menuClick;

            nacct = a;

            user = nacct.getUsername();
            pass = nacct.getPass();
            usertype = nacct.getUsertype();

            date = d;
            time = t;
            lUser.Text = "Welcome, " + user + ".";

            Reminders.Text = "Reminders:\n";
            clockTimer.Start();

            if (DateTime.Today.DayOfWeek == DayOfWeek.Thursday)
            {
                remPayroll.ForeColor = Color.Red;
                remPayroll.Text = "- Print Payroll List\n";
            }
            else
            {
                remPayroll.Text = "";
            }

            if (DateTime.Today.Day == 1)
            {
                remBIR.ForeColor = Color.Red;
                remBIR.Text = "- Print BIR Report\n";
            }
            else
            {
                remBIR.Text = "";
            }

            if (usertype == true)
            {
                userType = "Administrator Rights";
            }
            else
            {
                userType = "No Administrator Rights";
                acctViewAccts.Enabled = false;
            }

            acctUsername.Text = user;
            acctPassword.Text = "**************";
            acctUserType.Text = userType;

        }

        public String getloggeduser()
        {
            return user;
        }

        private void Start_Closing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                Application.Exit();
            }
        }

        private void logout_Click(object sender, EventArgs e)
        {
            DialogResult msg = MessageBox.Show("Are you sure you want to Log Out?", "Log Out", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);

            if (msg == DialogResult.Yes)
            {
                this.Dispose();

                login view = new login();
                view.ShowDialog();
            }
        }

        private void clockTimer_Tick(object sender, EventArgs e)
        {
            lDayOfWeek.Text = "Today is " + DateTime.Today.ToLongDateString() + ", " + DateTime.Now.ToLongTimeString();
        }

        /* -- Payroll Module -- */

        private void payGen_Click(object sender, EventArgs e)
        {
            panelPayUpdate.Visible = false;
            payBoxGenEmpName.Items.Clear();
            List<string> emplist = new List<string>();
            panelPayView.Visible = false;
            panelGenerate.Visible = true;
            payGenCV.Enabled = false;
            viewGeneratedCV.Visible = false;

            emplist = en.ViewEmployee();

            for(int i = 0; i < emplist.Count; i++)
            {
                payBoxGenEmpName.Items.Add(emplist[i]);
            }
        }

        private void payView_Click(object sender, EventArgs e)
        {
            panelPayUpdate.Visible = false;
            panelGenerate.Visible = false;
            panelPayView.Visible = true;
            paySearchBar.Enabled = false;
            payPrintSearch.Enabled = false;
            payPrintDataCV.Visible= false;
            payPrintDataPList.Visible = false;
            payPrintListBIR.Visible = false;
        }

        private void payUpdate_Click(object sender, EventArgs e)
        {
            panelGenerate.Visible = false;
            panelPayView.Visible = false;
            panelPayUpdate.Visible = true;
            panelPayAdd.Visible = false;
            panelPayModify.Visible = false;
        }

        private void payGenCV_Click(object sender, EventArgs e)
        {
            DialogResult rm = MessageBox.Show("Add cash advances?", "Cash Advances", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (rm == DialogResult.Yes)
            {
                form = this;
                cashadvances a = new cashadvances();
                a.Show();
                this.Enabled = false;
            }
            else if (rm == DialogResult.No)
            {
                advances = 0;
                paycreateCV();
            }
        }

        public void paysetAdvances(int a)
        {
            advances = a;
            paycreateCV();
        }

        private void paycreateCV()
        {
            ArrayList assigns = new ArrayList();
            String s;
            string[] remove;
            String no;
            int x;
            int y = Convert.ToInt32(empname);
            bool check = false;
            DataTable cv = new DataTable();
            DateTime date = DateTime.Now;

            check = paym.checkPay(y);

            if (payListGenerateAss.Items.Count > 0)
            {
                if (check)
                {
                    for (int i = 0; i < payListGenerateAss.Items.Count; i++)
                    {
                        s = payListGenerateAss.Items[i].ToString();
                        remove = s.Split('-');
                        no = remove[0];
                        x = Convert.ToInt32(no);
                        assigns.Add(x);
                    }

                    paym.CreateVoucher(assigns, y, advances);
                    cv = paym.viewVoucher(y, date);
                    viewGeneratedCV.DataSource = cv;
                    viewGeneratedCV.Visible = true;
                }
                else
                {
                    MessageBox.Show("Unable to create cash voucher because last payment was created less than 7 days ago", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
            else
            {
                MessageBox.Show("No available assignments", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void payGenSearch_Click(object sender, EventArgs e)
        {
           payListGenerateAss.Items.Clear();
           List<string> accassign = new List<string>();

           if (payBoxGenEmpName.SelectedIndex != -1)
           {
               accassign = en.ViewAccAssignments(empname);
               for (int i = 0; i < accassign.Count; i++)
               {
                   payListGenerateAss.Items.Add(accassign[i]);
               }
               payGenCV.Enabled = true;
           }
           else
           {
               MessageBox.Show("No employee selected","Error",MessageBoxButtons.OK, MessageBoxIcon.Stop);
           }
        }

        private void payPrintCVouch_Click(object sender, EventArgs e)
        {
            voucherdate f = new voucherdate();
            f.Show();
            form = this;
            this.Enabled = false;
            paySearchBar.Enabled = true;
            payPrintSearch.Enabled = true;
            payPrintDataCV.Visible = true;
            payPrintDataPList.Visible = false;
            payPrintListBIR.Visible = false;
        }

        public void setCVDate(DateTime d)
        {
            cvdate = d;
        }

        private void payPrintPList_Click(object sender, EventArgs e)
        {
            payrolldate f = new payrolldate();
            f.Show();
            form = this;
            this.Enabled = false;
            paySearchBar.Text = "";
            paySearchBar.Enabled = false;
            payPrintSearch.Enabled = false;
            payPrintDataCV.Visible = false;
            payPrintDataPList.Visible = true;
            payPrintListBIR.Visible = false;
        }

        public void getPList(DateTime a, DateTime b)
        {
            payDataDisplay.DataSource = null;
            listbegin = a;
            listend = b;
            plist = paym.viewList(listbegin, listend);

            payDataDisplay.DataSource = plist;
        }

        private void payPrintBIR_Click(object sender, EventArgs e)
        {
            birmonth f = new birmonth();
            f.Show();
            form = this;
            this.Enabled = false;
            paySearchBar.Text = "";
            paySearchBar.Enabled = false;
            payPrintSearch.Enabled = false;
            payPrintDataCV.Visible = false;
            payPrintDataPList.Visible = false;
            payPrintListBIR.Visible = true;
        }

        public void getBIR(int m)
        {
            payDataDisplay.DataSource = null;
            int month = m;
            bir = paym.viewBIR(m);

            payDataDisplay.DataSource = bir;
        }

        private void payPrintData_Click(object sender, EventArgs e)
        {
            DialogResult msg = MessageBox.Show("Are you sure you want to print?", "Print", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);

            if (msg == DialogResult.Yes)
            {
                msg = MessageBox.Show("Report Printed", "Print", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

            }
        }

        private void payBoxGenEmpName_SelectedIndexChanged(object sender, EventArgs e)
        {
            String item = payBoxGenEmpName.SelectedItem.ToString();
            String name2, id2;
            string[] words1 = item.Split('-');

            name2 = words1[0];
            id2 = words1[1];

            empname = id2;
        }

        private void payPrintSearch_Click(object sender, EventArgs e)
        {
            try
            {
                payDataDisplay.DataSource = null;
                int empid = Convert.ToInt32(paySearchBar.Text);
                cv = paym.viewVoucher(empid, cvdate);
            }
            catch (Exception err)
            {
                MessageBox.Show("Enter a valid ID", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }

            payDataDisplay.DataSource = cv;
        }

        /* -- Employee Module -- */

        private void empView_Click(object sender, EventArgs e)
        {
            ViewBox.Visible = true;
            AddBox.Visible = false;
            EditBox.Visible = false;
          //  empViewList.Items.Clear();
           // empViewList.SelectedIndex = -1;
            empViewList.DataSource = en.ViewEmployee();

            empViewID.Text = null;
            empViewTIN1.Text = null;
            empViewTIN2.Text = null;
            empViewTIN3.Text = null;
            empViewAddress.Text = null;
            empViewCity.Text = null;
            empViewContact.Text = null;

            empViewList.SelectedIndex = -1;
        }

        private void empAdd_Click(object sender, EventArgs e)
        {
            ViewBox.Visible = false;
            AddBox.Visible = true;
            EditBox.Visible = false;

            empAddTIN1.Text = null;
            empAddTIN2.Text = null;
            empAddTIN3.Text = null;
            empAddName.Text = null;
            empAddAddress.Text = null;
            empAddCity.Text = null;
            empAddContact.Text = null;
        }

        private void empEdit_Click(object sender, EventArgs e)
        {
            ViewBox.Visible = false;
            AddBox.Visible = false;
            EditBox.Visible = true;
           // empEditList.Items.Clear();
            empEditID.Text = null;
            empEditTIN1.Text = null;
            empEditTIN2.Text = null;
            empEditTIN3.Text = null;
            empEditAddress.Text = null;
            empEditCity.Text = null;
            empEditContact.Text = null;

           
             empEditList.DataSource = assm.EmployeeAssign();
             empEditID.Text = null;
             empEditTIN1.Text = null;
             empEditTIN2.Text = null;
             empEditTIN3.Text = null;
             empEditAddress.Text = null;
             empEditCity.Text = null;
             empEditContact.Text = null;
             empEditList.SelectedIndex = -1;
        }

        private void empVChangeStatus_Click(object sender, EventArgs e)
        {
            if (currentStatus == true)
            {
                empVChangeStatus.Text = "Terminate";

                DialogResult msg = MessageBox.Show("Are you sure you terminate this employee?", "Terminate Employee", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);

                if (msg == DialogResult.Yes)
                {
                    msg = MessageBox.Show("Employee Terminated.", "Terminate Employee", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    currentStatus = false;

                    empVChangeStatus.Text = "Hire Back";
                }
            }
            else if (currentStatus == false)
            {
                empVChangeStatus.Text = "Hire Back";

                DialogResult msg = MessageBox.Show("Are you sure you hire this employee back?", "Hire Back Employee", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);

                if (msg == DialogResult.Yes)
                {
                    msg = MessageBox.Show("Employee Hired Back.", "Hire Back Employee", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    currentStatus = true;

                    empVChangeStatus.Text = "Terminate";
                }
            }
        }

        private void empViewAssignments_Click(object sender, EventArgs e)
        {

        }

        private void empAddConf_Click(object sender, EventArgs e)
        {
            if (empAddTIN1.Text == "" || empAddTIN2.Text == "" || empAddTIN3.Text == "" || empAddName.Text == "" || empAddAddress.Text == "" || empAddContact.Text == "")
            {
                DialogResult msg = MessageBox.Show("Please make sure that the fields are not empty.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
            else
            {
                DialogResult msg = MessageBox.Show("Employee Added to Database.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

                empAddTIN1.Text = "";
                empAddTIN2.Text = "";
                empAddTIN3.Text = "";
                empAddName.Text = "";
                empAddAddress.Text = "";
                empAddContact.Text = "";
            }
        }

        private void empAddCancel_Click(object sender, EventArgs e)
        {
            empAddTIN1.Text = "";
            empAddTIN2.Text = "";
            empAddTIN3.Text = "";
            empAddName.Text = "";
            empAddAddress.Text = "";
            empAddContact.Text = "";
        }

        private void empEditConf_Click(object sender, EventArgs e)
        {
            if (empEditTIN1.Text == "" || empEditTIN2.Text == "" || empEditTIN3.Text == "" || empEditID.Text == "" || empEditAddress.Text == "" || empEditContact.Text == "")
            {
                DialogResult msg = MessageBox.Show("Please make sure that the fields are not Empty.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
            else
            {
                DialogResult msg = MessageBox.Show("Employee Edited.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

                empEditTIN1.Text = "";
                empEditTIN2.Text = "";
                empEditTIN3.Text = "";
                empEditID.Text = "";
                empEditAddress.Text = "";
                empEditContact.Text = "";
            }
        }

        private void empEditCancel_Click(object sender, EventArgs e)
        {
            empEditTIN1.Text = "";
            empEditTIN2.Text = "";
            empEditTIN3.Text = "";
            empEditID.Text = "";
            empEditAddress.Text = "";
            empEditContact.Text = "";
        }

        /* -- Account Module -- */

        private void acctEdit_Click(object sender, EventArgs e)
        {
            panelEditAccount.Visible = true;
            panelLoginHistory.Visible = false;
            panelViewAccts.Visible = false;
        }

        private void acctEditPassword_Click(object sender, EventArgs e)
        {
            panelTerminate.Visible = false;
            panelEditSecurity.Visible = false;
            panelEditPassword.Visible = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            panelTerminate.Visible = true;
            panelEditPassword.Visible = false;
            panelEditSecurity.Visible = false;
        }

        private void acctViewHistory_Click(object sender, EventArgs e)
        {
            panelEditAccount.Visible = false;
            panelLoginHistory.Visible = true;
            panelViewAccts.Visible = false;

            //--------------------------------

            acctLoginHistory.DataSource = acctm.viewLoginHistory();
        }

        private void acctViewAccts_Click(object sender, EventArgs e)
        {
            panelEditAccount.Visible = false;
            panelLoginHistory.Visible = false;
            panelViewAccts.Visible = true;

            //--------------------------------

            acctViewUser.DataSource = acctm.viewUserAccounts();
        }

        public void acctRefresh()
        {
            acctViewUser.DataSource = acctm.viewUserAccounts();
        }

        private void acctEditSecurity_Click(object sender, EventArgs e)
        {
            panelTerminate.Visible = false;
            panelEditPassword.Visible = false;
            panelEditSecurity.Visible = true;
        }

        private void acctChangePassword_Click(object sender, EventArgs e)
        {
            String newpass = "";
            if (pass == acctCurPassword.Text && acctVerifyPassword.Text == acctNewPassword.Text && acctNewPassword.Text != pass && acctNewPassword.Text != "" && acctVerifyPassword.Text != "")
            {
                DialogResult msg = MessageBox.Show("Are you sure you want to change your password?", "Update Password", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);

                if (msg == DialogResult.Yes)
                {
                    msg = MessageBox.Show("Password has been successfully updated, you will be automatically logged out for the changes to take effect", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    newpass = acctNewPassword.Text;
                    acctm.editPassword(user, newpass);

                    acctCurPassword.Text = "";
                    acctVerifyPassword.Text = "";
                    acctNewPassword.Text = "";

                    this.Dispose();

                    login view = new login();
                    view.ShowDialog();
                }
                else
                {
                    acctCurPassword.Text = "";
                    acctVerifyPassword.Text = "";
                    acctNewPassword.Text = "";
                }
            }
            else if (acctCurPassword.Text == "" || acctNewPassword.Text == "" || acctVerifyPassword.Text == "")
            {
                MessageBox.Show("Please make sure that all fields are properly encoded", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                acctCurPassword.Text = "";
                acctVerifyPassword.Text = "";
                acctNewPassword.Text = "";
            }
            else if (pass != acctCurPassword.Text)
            {
                MessageBox.Show("Password is does not match with this account", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                acctCurPassword.Text = "";
                acctVerifyPassword.Text = "";
                acctNewPassword.Text = "";
            }
            else if (acctNewPassword.Text != acctVerifyPassword.Text)
            {
                MessageBox.Show("Please confirm that your new password is correct", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                acctCurPassword.Text = "";
                acctVerifyPassword.Text = "";
                acctNewPassword.Text = "";
            }
            else if (acctNewPassword.Text == pass)
            {
                MessageBox.Show("Please enter a new password different from your current password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                acctCurPassword.Text = "";
                acctVerifyPassword.Text = "";
                acctNewPassword.Text = "";
            }
        }

        private void acctChangeSecurity_Click(object sender, EventArgs e)
        {
            String newquest = "";
            String newans = "";
            if (pass == acctSecPass.Text && acctSecAns.Text != "" && acctSecQuest.Text != "")
            {
                DialogResult msg = MessageBox.Show("Are you sure you want to change your security parameters?", "Update Password", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);

                if (msg == DialogResult.Yes)
                {
                    msg = MessageBox.Show("Security Question and Answer have been successfully updated", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    newquest = acctSecQuest.Text;
                    newans = acctSecAns.Text;
                    acctm.editSecurity(user, newquest, newans);

                    acctSecPass.Text = "";
                    acctSecQuest.Text = "";
                    acctSecAns.Text = "";
                }
                else
                {
                    acctSecPass.Text = "";
                    acctSecQuest.Text = "";
                    acctSecAns.Text = "";
                }
            }
            else if (acctSecPass.Text == "" || acctSecQuest.Text == "" || acctSecAns.Text == "")
            {
                MessageBox.Show("Please make sure that all fields are properly encoded", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                acctSecPass.Text = "";
                acctSecQuest.Text = "";
                acctSecAns.Text = "";
            }
            else if (pass != acctSecPass.Text)
            {
                MessageBox.Show("Password is does not match with this account", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                acctSecPass.Text = "";
                acctSecQuest.Text = "";
                acctSecAns.Text = "";
            }
        }

        private void acctTerminateConf_Click(object sender, EventArgs e)
        {
            bool check = false;
            if (pass == acctTermPassword.Text)
            {
                DialogResult msg = MessageBox.Show("Are you sure you want to terminate your account?", "Terminate Account", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);

                if (msg == DialogResult.Yes)
                {
                    check = acctm.findAccount();

                    if (check)
                    {
                        msg = MessageBox.Show("Account Terminated. You will now be logged off the system.", "Account Terminated", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                        acctm.terminateAccount(user);
                        this.Dispose();

                        login view = new login();
                        view.ShowDialog();
                    }
                    else
                    {
                        MessageBox.Show("Unable to terminate account because this account is the only administrator present in the system", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        acctTermPassword.Text = "";
                    }
                }
                else
                {
                    acctTermPassword.Text = "";
                }
            }
            else if (acctTermPassword.Text == "")
            {
                MessageBox.Show("Please enter a valid password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                acctTermPassword.Text = "";
            }
            else if (pass != acctTermPassword.Text)
            {
                MessageBox.Show("Password is does not match with this account", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                acctTermPassword.Text = "";
            }
        }

        private void acctCreateAcct_Click(object sender, EventArgs e)
        {
            form = this;

            createAccount create = new createAccount();
            create.Show();

            this.Enabled = false;
        }

        private void acctEditAccts_Click(object sender, EventArgs e)
        {
            form = this;

            editAccount edit = new editAccount();
            edit.Show();

            this.Enabled = false;
        }

        private void acctHome_Paint(object sender, PaintEventArgs e)
        {

        }

        /* -- Assignment Module -- */

        private void Print_Click(object sender, EventArgs e)
	        {
                try
                {
                    Excel.Application xlApp;
                    Excel.Workbook xlWorkBook;
                    Excel.Worksheet xlWorkSheet;
                    object misValue = System.Reflection.Missing.Value;

                    xlApp = new Excel.ApplicationClass();
                    xlWorkBook = xlApp.Workbooks.Add(misValue);
                    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                    int i = 0;
                    int j = 0;

                    for (i = 0; i <= assEditDisplay.RowCount - 1; i++)
                    {
                        for (j = 0; j <= assEditDisplay.ColumnCount - 1; j++)
                        {
                            DataGridViewCell cell = assEditDisplay[j, i];
                            xlWorkSheet.Cells[i + 1, j + 1] = cell.Value;
                        }
                    }

                    xlWorkBook.SaveAs("masterlist.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                    xlWorkBook.Close(true, misValue, misValue);
                    xlApp.Quit();

                    releaseObject(xlWorkSheet);
                    releaseObject(xlWorkBook);
                    releaseObject(xlApp);

                    MessageBox.Show("Excel file created , you can find the file in My Documents");
                }
                catch (Exception err4)
                {
                    MessageBox.Show("Error occured while generating Excel file + " + err4.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
	        }
	
	    private void releaseObject(object obj)
	        {
	            try
	            {
	                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
	                obj = null;
	            }
	            catch (Exception ex)
	            {
	                obj = null;
	                MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
	            }
	            finally
	            {
	                GC.Collect();
	            }
	        }
	
	    private void Print2_Click(object sender, EventArgs e)
	        {
                try
                {
                    Excel.Application xlApp;
                    Excel.Workbook xlWorkBook;
                    Excel.Worksheet xlWorkSheet;
                    object misValue = System.Reflection.Missing.Value;

                    xlApp = new Excel.ApplicationClass();
                    xlWorkBook = xlApp.Workbooks.Add(misValue);
                    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                    int i = 0;
                    int j = 0;

                    for (i = 0; i <= assViewDisplay.RowCount - 1; i++)
                    {
                        for (j = 0; j <= assViewDisplay.ColumnCount - 1; j++)
                        {
                            DataGridViewCell cell = assViewDisplay[j, i];
                            xlWorkSheet.Cells[i + 1, j + 1] = cell.Value;
                        }
                    }

                    xlWorkBook.SaveAs("masterlist.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                    xlWorkBook.Close(true, misValue, misValue);
                    xlApp.Quit();

                    releaseObject(xlWorkSheet);
                    releaseObject(xlWorkBook);
                    releaseObject(xlApp);

                    MessageBox.Show("Excel file created , you can find the file in My Documents");
                }
                catch (Exception err4)
                {
                    MessageBox.Show("Error occured while generating Excel file + "+err4.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Stop);
                }
	        }

        private void assView_Click(object sender, EventArgs e)
        {
            panelAssView.Visible = true;
            panelAssEdit.Visible = false;
            panelAssAdd.Visible = false;

            assViewFilter.Items.Clear();
            assViewFilter.Text = "Select Filter";
            assViewFilter.Items.Add("Assign ID");
            assViewFilter.Items.Add("Employee ID");
            assViewFilter.Items.Add("Employee Name");
            assViewFilter.Items.Add("Requesting Unit");
            assViewFilter.Items.Add("Description");
            assViewFilter.Items.Add("Batch No");
            assViewFilter.Items.Add("Status");
            assViewFilter.Items.Add("Start Date");
            assViewFilter.Items.Add("End Date");
            assViewFilter.Items.Add("Address");
            assViewFilter.Items.Add("City");
            //--------------------------

            assViewDisplay.DataSource = assm.ViewAssign();
        }

        private void assAdd_Click(object sender, EventArgs e)
        {
            List<string> a = new List<string>();
            a = assm.getRates();
            panelAssView.Visible = false;
            panelAssEdit.Visible = false;
            panelAssAdd.Visible = true;

            assAddRequestingUnit.Text = null;
            assAddBatchNo.Text = null;
            startDate.Text = null;
            endDate.Text = null;
            assAddAddress.Text = null;
            assAddCity.Text = null;
            assAddDesc.Text = null;
            Type1.Text = "Select Type";
            Type1.Items.Clear();
            Type1.Text = "Select Type";


            for (int i = 0; i < a.Count; i++)
            {
                Type1.Items.Add(a[i]);
            }

            AssignBox.DataSource = assm.EmployeeAssign();
            AssignBox.SelectedIndex = -1;
        }

        private void assEdit_Click(object sender, EventArgs e)
        {
            panelAssView.Visible = false;
            panelAssEdit.Visible = true;
            panelAssAdd.Visible = false;

            assEditFilter.Items.Clear();
            assEditFilter.Text = "Select Filter";
            assEditFilter.Items.Add("Assign ID");
            assEditFilter.Items.Add("Employee ID");
            assEditFilter.Items.Add("Employee Name");
            assEditFilter.Items.Add("Requesting Unit");
            assEditFilter.Items.Add("Description");
            assEditFilter.Items.Add("Batch No");
            assEditFilter.Items.Add("Status");
            assEditFilter.Items.Add("Start Date");
            assEditFilter.Items.Add("End Date");
            assEditFilter.Items.Add("Address");
            assEditFilter.Items.Add("City");

            assEditDisplay.DataSource =assm.ViewTable();
            sana = assm.ViewTable();
        }

        public void assEditDisplay_Refresh()
        {
            assEditDisplay.DataSource = assm.ViewTable();
            sana = assm.ViewTable();
            //assEditFilter.Text = "Select Filter";
            assEFilter.Text = "";
        }

        private void assEditCancel_Click(object sender, EventArgs e)
        {

        }

        private void assEditSave_Click(object sender, EventArgs e)
        {

        }

        private void assEditConf_Click(object sender, EventArgs e)
        {
            Assignment n = new Assignment();


            //DataRow dataRow = assm.ViewTable().Rows[assEditDisplay.CurrentRow.Index];

            DataRow dataRow = sana.Rows[assEditDisplay.CurrentRow.Index];

            n.AssignId = Convert.ToString(dataRow["Assignment No."]);
            n.Eid = Convert.ToString(dataRow["Employee ID"]);
            n.Ename = Convert.ToString(dataRow["Employee"]);
            n.Request = Convert.ToString(dataRow["Requesting Unit"]);
            n.Assign = Convert.ToString(dataRow["Description"]);
            n.Batch = Convert.ToString(dataRow["Batch No."]);
            n.Status = Convert.ToString(dataRow["Status"]);
            n.Startdate = Convert.ToString(dataRow["Start Date"]);
            n.Enddate = Convert.ToString(dataRow["End Date"]);
            n.Address = Convert.ToString(dataRow["Address"]);
            n.City = Convert.ToString(dataRow["City/Municipality"]);
            n.Type = Convert.ToString(dataRow["Type"]);

            form = this;

            this.Enabled = false;

            Form2 edit = new Form2(n);
            edit.Show();
           
        }

        private void assEditCancel_Click_1(object sender, EventArgs e)
        {

        }

        private void empAddConf_Click_1(object sender, EventArgs e)
        {
            if (empAddTIN1.Text == "" || empAddTIN2.Text == "" || empAddTIN3.Text == "" || empAddName.Text == "" || empAddAddress.Text == "" || empAddContact.Text == "" || empAddCity.Text == "")
            {
                DialogResult msg = MessageBox.Show("Incomplete Form", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                if (empAddTIN1.TextLength == 3 && empAddTIN2.TextLength == 3 && empAddTIN3.TextLength == 3)
                {
                    Employee thisEmployee = new Employee();

                    String dash = "-";

                    String TIN1 = empAddTIN1.Text, TIN2 = empAddTIN2.Text, TIN3 = empAddTIN3.Text;
                    thisEmployee.TIN = TIN1 += dash += TIN2 += dash += TIN3;
                    thisEmployee.Name = empAddName.Text;
                    thisEmployee.Address = empAddAddress.Text;
                    thisEmployee.City = empAddCity.Text;
                    thisEmployee.ContactNo = empAddContact.Text;
                    thisEmployee.Status = "1";

                    en.InsertEmployee(thisEmployee);
                    DialogResult msg1 = MessageBox.Show("The Employee has been added to the Database", "Add", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    empAddTIN1.Text = null;
                    empAddTIN2.Text = null;
                    empAddTIN3.Text = null;
                    empAddName.Text = null;
                    empAddAddress.Text = null;
                    empAddCity.Text = null;
                    empAddContact.Text = null;
                }
                else
                {
                    DialogResult msg = MessageBox.Show("Incorrect TIN", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void empEditList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (empEditList.SelectedIndex != -1)
            {
                String item = empEditList.Text;
                String name2, id2;
                string[] words1 = item.Split('-');

                name2 = words1[0];
                id2 = words1[1];

                string tin1;
                string add1;
                int con1;
                string city1;

                empEditID.Text = words1[0];
                tin1 = en.EmployeeTIN(id2);
                string[] words = tin1.Split('-');
                empEditTIN1.Text = words[0];
                empEditTIN2.Text = words[1];
                empEditTIN3.Text = words[2];

                add1 = en.EmployeeAddress(id2);
                empEditAddress.Text = add1;
                city1 = en.EmployeeCity(id2);
                empEditCity.Text = city1;
                con1 = en.EmployeeContact(id2);
                empEditContact.Text = Convert.ToString(con1);
            }
        }

        private void empEditConf_Click_1(object sender, EventArgs e)
        {
            if (empEditTIN1.Text == "" || empEditTIN2.Text == "" || empEditTIN3.Text == "" || empEditList.Text == "" || empEditAddress.Text == "" || empEditCity.Text == "" || empEditContact.Text == "" )
            {
                DialogResult msg = MessageBox.Show("Incomplete Form", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                if (empEditTIN1.TextLength == 3 && empEditTIN2.TextLength == 3 && empEditTIN3.TextLength == 3)
                {
                    Employee thisEmployee = new Employee();

                    String dash = "-";

                    String item = empEditList.Text;
                    String name2, id2;
                    string[] words1 = item.Split('-');

                    name2 = words1[0];
                    id2 = words1[1];

                    thisEmployee.IdNo = id2;
                    thisEmployee.TIN = empEditTIN1.Text += dash += empEditTIN2.Text += dash += empEditTIN3.Text;
                    thisEmployee.Name = empEditID.Text;
                    thisEmployee.Address = empEditAddress.Text;
                    thisEmployee.City = empEditCity.Text;
                    thisEmployee.ContactNo = empEditContact.Text;

                    en.EditEmployee(thisEmployee);
                    DialogResult msg1 = MessageBox.Show("The Employee Information has been updated in the Database", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
                 

                    //empEditList.Items.Clear();
                    empEditList.SelectedIndex = -1;
                    empEditList.DataSource = assm.EmployeeAssign();
                    empEditID.Text = null;
                    empEditTIN1.Text = null;
                    empEditTIN2.Text = null;
                    empEditTIN3.Text = null;
                    empEditAddress.Text = null;
                    empEditCity.Text = null;
                    empEditContact.Text = null;

                    empEditList.SelectedIndex = -1;

                }
                else
                {
                    DialogResult msg = MessageBox.Show("Incorrect TIN", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void empViewList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (empViewList.SelectedIndex != -1)
            {
                String item = empViewList.Text;
                String name2, id2;
                string[] words1 = item.Split('-');

                name2 = words1[0];
                id2 = words1[1];


                string tin1;
                string add1;
                int con1;
                string city1;

                empViewID.Text = id2;
                tin1 = en.EmployeeTIN(id2);
                string[] words = tin1.Split('-');
                empViewTIN1.Text = words[0];
                empViewTIN2.Text = words[1];
                empViewTIN3.Text = words[2];

                add1 = en.EmployeeAddress(id2);
                empViewAddress.Text = add1;
                city1 = en.EmployeeCity(id2);
                empViewCity.Text = city1;
                con1 = en.EmployeeContact(id2);
                empViewContact.Text = Convert.ToString(con1);
            }
         
        }

        private void empViewList_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            if (empViewList.SelectedIndex != -1)
            {
                String item = empViewList.Text;
                String name2, id2;
                string[] words1 = item.Split('-');



                name2 = words1[0];
                id2 = words1[1];


                string tin1;
                string add1;
                int con1;
                string city1;

                empViewID.Text = name2;
                tin1 = en.EmployeeTIN(id2);
                string[] words = tin1.Split('-');
                empViewTIN1.Text = words[0];
                empViewTIN2.Text = words[1];
                empViewTIN3.Text = words[2];

                add1 = en.EmployeeAddress(id2);
                empViewAddress.Text = add1;
                city1 = en.EmployeeCity(id2);
                empViewCity.Text = city1;
                con1 = en.EmployeeContact(id2);
                empViewContact.Text = Convert.ToString(con1);

                empAssList.Visible = false;
            }
        }

        private void empVChangeStatus_Click_1(object sender, EventArgs e)
        {
            if (empViewList.Text == "")
            {
                DialogResult msg = MessageBox.Show("Incomplete Form", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                Employee thisEmployee = new Employee();
                Assignment thisAssignment = new Assignment();

                String item = empViewList.Text;
                String name2, id2, id3;
                int id1;
                string[] words1 = item.Split('-');

                name2 = words1[0];
                id2 = words1[1];

                if (en.EmployeeBoolean(id2))
                {

                    String dash = "-";
                    String dash1 = "-";
                    String Terminate = "*Terminated";

                    thisEmployee.IdNo = id2;
                    thisAssignment.Eid = id2;
                    thisEmployee.Name = name2 += Terminate;
                    thisAssignment.Ename = name2;
                    thisEmployee.Status = "0";

                    en.TerminateEmployee(thisEmployee);
                    assm.UpdateAssign(thisAssignment);
                    DialogResult msg1 = MessageBox.Show("The Employee has been Terminate", "Terminate", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    //empViewList.Items.Clear();

                    empViewID.Text = words1[0] += Terminate += dash1 += words1[1];

                    empViewList.DataSource = en.ViewEmployee();
                }
                else
                {
                    DialogResult msg1 = MessageBox.Show("Already Terminated", "GRABE", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void Hire_Click(object sender, EventArgs e)
        {
            if (empViewList.Text == "")
            {
                DialogResult msg = MessageBox.Show("Incomplete Form", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                String item = empViewList.Text;
                String name2, id2;
                int id1;
                string[] words1 = item.Split('-');

                name2 = words1[0];
                id2 = words1[1];

                if (!en.EmployeeBoolean(id2))
                {
                    Employee thisEmployee = new Employee();
                    Assignment thisAssignment = new Assignment();
                    DialogResult msg1 = MessageBox.Show("The Employee has been Hired Back", "Add", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    string name1;

                    name1 = name2;

                    string[] words = name1.Split('*');

                    thisEmployee.Name = words[0];
                    thisAssignment.Ename = words[0];
                    thisEmployee.IdNo = id2;
                    thisAssignment.Eid = id2;
                    thisEmployee.Status = "1";

                    en.HireEmployee(thisEmployee);
                    assm.UpdateAssign(thisAssignment);

                  //  empViewList.Items.Clear();

                    String dash = "-";
                    empViewID.Text = words[0] += dash += id2;
                    empViewList.DataSource = en.ViewEmployee();
                }
                else
                {
                    DialogResult msg1 = MessageBox.Show("Already Hired", "GRABE", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void assAddConf_Click(object sender, EventArgs e)
        {
            if (assAddRequestingUnit.Text == "" || assAddAddress.Text == "" || assAddCity.Text == "" || assAddBatchNo.Text == "" || assAddDesc.Text == "" || startDate.Text == "" || endDate.Text == "" || Type1.Text == "Select Type")
            {
                DialogResult msg = MessageBox.Show("Incomplete Form", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                
               if(Convert.ToDateTime(startDate.Text) > Convert.ToDateTime(endDate.Text))
                {
                    DialogResult msg = MessageBox.Show("Incorrect Date", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                

                    else 
                    {
                        Assignment thisAssignment = new Assignment();

                        String dash = "-";
                        String type = Type1.SelectedItem.ToString();
                        String i = "";
                        string[] word2 = type.Split('-');

                        if (AssignBox.SelectedIndex != -1)
                        {
                            String item = AssignBox.Text;
                            String name2, id2;
                            int id1;
                            string[] words1 = item.Split('-');

                            name2 = words1[0];
                            id2 = words1[1];

                            thisAssignment.Eid = id2;
                            thisAssignment.Ename = name2;
                        }
                        else
                        {
                            thisAssignment.Eid = "";
                            thisAssignment.Ename = "";
                        }

                        i = word2[0];

                        thisAssignment.Request = assAddRequestingUnit.Text;
                        thisAssignment.Batch = assAddBatchNo.Text;
                        thisAssignment.Startdate = startDate.Text;
                        thisAssignment.Enddate = endDate.Text;
                        thisAssignment.Address = assAddAddress.Text;
                        thisAssignment.City = assAddCity.Text;
                        thisAssignment.Assign = assAddDesc.Text;
                        thisAssignment.Status = "New";
                       // thisAssignment.Ename = name2;
                        thisAssignment.Type = i;

                        assm.InsertAssign(thisAssignment);
                        DialogResult msg1 = MessageBox.Show("Assignment has been added to the Database", "Add", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        /*assAddRequestingUnit.Text = null;
                        assAddBatchNo.Text = null;
                        startDate.Text = null;
                        endDate.Text = null;
                        assAddAddress.Text = null;
                        assAddCity.Text = null;
                        assAddDesc.Text = null;*/
                    }
                
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            String parameter = "";
            String dash = "-";
            dt = new DataTable();
            dt.Columns.Add(new DataColumn("Assignment No.", typeof(int)));
            dt.Columns.Add(new DataColumn("Requesting Unit", typeof(string)));
            dt.Columns.Add(new DataColumn("Batch No.", typeof(int)));
            dt.Columns.Add(new DataColumn("Start Date", typeof(string)));
            dt.Columns.Add(new DataColumn("End Date", typeof(string)));
            dt.Columns.Add(new DataColumn("Address", typeof(string)));
            dt.Columns.Add(new DataColumn("City/Municipality", typeof(string)));
            dt.Columns.Add(new DataColumn("Employee", typeof(string)));
            dt.Columns.Add(new DataColumn("Employee Id", typeof(int)));
            dt.Columns.Add(new DataColumn("Description", typeof(string)));
            dt.Columns.Add(new DataColumn("Status", typeof(string)));


            if (assEFilter.Text == "" || assEditFilter.Text == "Select Filter")
            {
                DialogResult msg = MessageBox.Show("Incomplete Form", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                if (assEditFilter.SelectedItem.Equals("Employee Name"))
                {
                    parameter = "e_name";
                    assEditDisplay.DataSource = assm.FilterAssign(assEFilter.Text, parameter);
                    sana = assm.FilterAssign(assEFilter.Text, parameter);
                }

                if (assEditFilter.SelectedItem.Equals("Employee ID"))
                {
                    parameter = "e_id";
                    assEditDisplay.DataSource = assm.FilterAssign(assEFilter.Text, parameter);
                    sana = assm.FilterAssign(assEFilter.Text, parameter);
                }

                if (assEditFilter.SelectedItem.Equals("Assign ID"))
                {
                    parameter = "assign_id";
                    assEditDisplay.DataSource = assm.FilterAssign(assEFilter.Text, parameter);
                    sana = assm.FilterAssign(assEFilter.Text, parameter);
                }

                if (assEditFilter.SelectedItem.Equals("Requesting Unit"))
                {
                    parameter = "requestingunit";
                    assEditDisplay.DataSource = assm.FilterAssign(assEFilter.Text, parameter);
                    sana = assm.FilterAssign(assEFilter.Text, parameter);
                }

                if (assEditFilter.SelectedItem.Equals("Description"))
                {
                    parameter = "assignment";
                    assEditDisplay.DataSource = assm.FilterAssign(assEFilter.Text, parameter);
                    sana = assm.FilterAssign(assEFilter.Text, parameter);
                }

                if (assEditFilter.SelectedItem.Equals("Batch No"))
                {
                    parameter = "batchno";
                    assEditDisplay.DataSource = assm.FilterAssign(assEFilter.Text, parameter);
                    sana = assm.FilterAssign(assEFilter.Text, parameter);
                }

                if (assEditFilter.SelectedItem.Equals("Status"))
                {
                    parameter = "status";
                    assEditDisplay.DataSource = assm.FilterAssign(assEFilter.Text, parameter);
                    sana = assm.FilterAssign(assEFilter.Text, parameter);
                }

                if (assEditFilter.SelectedItem.Equals("Start Date"))
                {
                    parameter = "startdate";
                    assEditDisplay.DataSource = assm.FilterAssign(assEFilter.Text, parameter);
                    sana = assm.FilterAssign(assEFilter.Text, parameter);
                }

                if (assEditFilter.SelectedItem.Equals("End Date"))
                {
                    parameter = "endate";
                    assEditDisplay.DataSource = assm.FilterAssign(assEFilter.Text, parameter);
                    sana = assm.FilterAssign(assEFilter.Text, parameter);
                }

                if (assEditFilter.SelectedItem.Equals("Address"))
                {
                    parameter = "address";
                    assEditDisplay.DataSource = assm.FilterAssign(assEFilter.Text, parameter);
                    sana = assm.FilterAssign(assEFilter.Text, parameter);
                }
                if (assEditFilter.SelectedItem.Equals("City"))
                {
                    parameter = "city";
                    assEditDisplay.DataSource = assm.FilterAssign(assEFilter.Text, parameter);
                    sana = assm.FilterAssign(assEFilter.Text, parameter);
                }
            }
        }

        private void assignViewSearch_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            String dash = "-";
            String parameter = "";
            dt = new DataTable();
            dt.Columns.Add(new DataColumn("Assignment No.", typeof(int)));
            dt.Columns.Add(new DataColumn("Requesting Unit", typeof(string)));
            dt.Columns.Add(new DataColumn("Batch No.", typeof(int)));
            dt.Columns.Add(new DataColumn("Start Date", typeof(string)));
            dt.Columns.Add(new DataColumn("End Date", typeof(string)));
            dt.Columns.Add(new DataColumn("Address", typeof(string)));
            dt.Columns.Add(new DataColumn("City/Municipality", typeof(string)));
            dt.Columns.Add(new DataColumn("Employee", typeof(string)));
            dt.Columns.Add(new DataColumn("Employee Id", typeof(int)));
            dt.Columns.Add(new DataColumn("Description", typeof(string)));
            dt.Columns.Add(new DataColumn("Status", typeof(string)));

            if (assVFilter.Text == "" || assViewFilter.Text=="Select Filter")
            {
                DialogResult msg = MessageBox.Show("Incomplete Form", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                if (assViewFilter.SelectedItem.Equals("Employee Name"))
                {
                    parameter = "e_name";
                    assViewDisplay.DataSource = assm.FilterAssign(assVFilter.Text, parameter);
                    
                }

                if (assViewFilter.SelectedItem.Equals("Employee ID"))
                {
                    parameter = "e_id";
                    assViewDisplay.DataSource = assm.FilterAssign(assVFilter.Text, parameter);
                }

                if (assViewFilter.SelectedItem.Equals("Assign ID"))
                {
                    parameter = "assign_id";
                    assViewDisplay.DataSource = assm.FilterAssign(assVFilter.Text, parameter);
                   
                }

                if (assViewFilter.SelectedItem.Equals("Requesting Unit"))
                {
                    parameter = "requestingunit";
                    assViewDisplay.DataSource = assm.FilterAssign(assVFilter.Text, parameter);
                }

                if (assViewFilter.SelectedItem.Equals("Description"))
                {
                    parameter = "assignment";
                    assViewDisplay.DataSource = assm.FilterAssign(assVFilter.Text, parameter);
                }

                if (assViewFilter.SelectedItem.Equals("Batch No"))
                {
                    parameter = "batchno";
                    assViewDisplay.DataSource = assm.FilterAssign(assVFilter.Text, parameter);
                }

                if (assViewFilter.SelectedItem.Equals("Status"))
                {
                    parameter = "status";
                    assViewDisplay.DataSource = assm.FilterAssign(assVFilter.Text, parameter);
                }

                if (assViewFilter.SelectedItem.Equals("Start Date"))
                {
                    parameter = "startdate";
                    assViewDisplay.DataSource = assm.FilterAssign(assVFilter.Text, parameter);
                }

                if (assViewFilter.SelectedItem.Equals("End Date"))
                {
                    parameter = "endate";
                    assViewDisplay.DataSource = assm.FilterAssign(assVFilter.Text, parameter);
                }

                if (assViewFilter.SelectedItem.Equals("Address"))
                {
                    parameter = "address";
                    assViewDisplay.DataSource = assm.FilterAssign(assVFilter.Text, parameter);
                }
                if (assViewFilter.SelectedItem.Equals("City"))
                {

                    parameter = "city";
                    assViewDisplay.DataSource = assm.FilterAssign(assVFilter.Text, parameter);
                }
            }
        }

        private void empViewAssignments_Click_1(object sender, EventArgs e)
        {
            empAssList.Visible = true;
            if (empViewList.Text == "")
            {
                DialogResult msg = MessageBox.Show("Incomplete Form", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
               /* string conn = "Data Source=GABS-PC\\SQLEXPRESS;Initial Catalog=DB_IHUB;Integrated Security=True";
                SqlConnection con = new SqlConnection(conn);
                SqlCommand cmd = con.CreateCommand();*/
                String item = empViewList.Text;
                String name2, id2;
                string[] words1 = item.Split('-');

             //   empAssList.Items.Clear();
                name2 = words1[0];
                id2 = words1[1];
               /* cmd.CommandText = "select * from Assignments where e_name = '" + name2 + "' AND e_id = '" + id2 + "'";
                con.Open();
                SqlDataReader dr = cmd.ExecuteReader();*/



              /*  while (dr.Read())
                {
                    String request = (string)dr.GetValue(3);
                    String assign = (string)dr.GetValue(4);
                    String status = (string)dr.GetValue(6);
                    String dash = "-";
                    String item1 = request += dash += assign += dash += status;
                    empAssList.Items.Add(item1);

                }*/

                empAssList.DataSource = en.ViewAssignments(id2);


             //   dr.Close();
               // con.Close();
            }
        }

        private void panelAssAdd_Paint(object sender, PaintEventArgs e)
        {

        }

        private void main_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dB_IHUBDataSet.Assignments' table. You can move, or remove it, as needed.

        }

        private void assEditDisplay_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //Assignment thisAssignment = new Assignment();
            //String Yeah;
            //thisAssignment.Request = assEditDisplay.SelectedRows.CopyTo(Yeah);
        }

        private void empEditCancel_Click_1(object sender, EventArgs e)
        {
            DialogResult msg1 = MessageBox.Show("Do you want to exit the Edit Employee Application?", "Edit", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
          //  DateTime date = DateTime.Now;
           // String dt = date.ToShortDateString();
           // String time = date.ToShortTimeString();
           // Account a = new Account();
            if (msg1 == DialogResult.Yes)
            {
                menu.SelectedIndex = 0; 
            }
        }

        private void homeScreen_Click(object sender, EventArgs e)
        {

        }

        private void assAddCancel_Click(object sender, EventArgs e)
        {
            DialogResult msg1 = MessageBox.Show("Do you want to Clear the Add Assignment Application?", "Edit", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            //  DateTime date = DateTime.Now;
            // String dt = date.ToShortDateString();
            // String time = date.ToShortTimeString();
            // Account a = new Account();
            if (msg1 == DialogResult.Yes)
            {
                assAddRequestingUnit.Text = null;
                assAddBatchNo.Text = null;
                startDate.Text = null;
                endDate.Text = null;
                assAddAddress.Text = null;
                assAddCity.Text = null;
                assAddDesc.Text = null;
                Type1.Text = "Select Type";
                AssignBox.DataSource = assm.EmployeeAssign();
                AssignBox.SelectedIndex = -1;
            }
        }

        private void empAddCancel_Click_1(object sender, EventArgs e)
        {
            DialogResult msg1 = MessageBox.Show("Do you want to exit the Add Employee Application?", "Add", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            //  DateTime date = DateTime.Now;
            // String dt = date.ToShortDateString();
            // String time = date.ToShortTimeString();
            // Account a = new Account();
            if (msg1 == DialogResult.Yes)
            {
                menu.SelectedIndex = 0; 
            }
        }

        private void AssignBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            Assignment n = new Assignment();

            DataRow dataRow = dt.Rows[assViewDisplay.CurrentRow.Index];

            n.AssignId = Convert.ToString(dataRow["Assignment No."]);
            n.Eid = Convert.ToString(dataRow["Employee ID"]);
            n.Ename = Convert.ToString(dataRow["Employee"]);
            n.Request = Convert.ToString(dataRow["Requesting Unit"]);
            n.Assign = Convert.ToString(dataRow["Description"]);
            n.Batch = Convert.ToString(dataRow["Batch No."]);
            n.Status = Convert.ToString(dataRow["Status"]);
            n.Startdate = Convert.ToString(dataRow["Start Date"]);
            n.Enddate = Convert.ToString(dataRow["End Date"]);
            n.Address = Convert.ToString(dataRow["Address"]);
            n.City = Convert.ToString(dataRow["City/Municipality"]);

            form = this;

            this.Enabled = false;

            Form2 edit = new Form2(n);
            edit.Show();
        }

        private void payPrintDataCV_Click(object sender, EventArgs e)
        {
            DialogResult msg = MessageBox.Show("Are you sure you want to print?", "Print", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);

            if (msg == DialogResult.Yes)
            {
                try
                {
                    Excel.Application xlApp;
                    Excel.Workbook xlWorkBook;
                    Excel.Worksheet xlWorkSheet;
                    object misValue = System.Reflection.Missing.Value;

                    xlApp = new Excel.ApplicationClass();
                    xlWorkBook = xlApp.Workbooks.Add(misValue);
                    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                    int i = 0;
                    int j = 0;

                    for (i = 0; i <= payDataDisplay.RowCount - 1; i++)
                    {
                        for (j = 0; j <= payDataDisplay.ColumnCount - 1; j++)
                        {
                            DataGridViewCell cell = payDataDisplay[j, i];
                            xlWorkSheet.Cells[i + 1, j + 1] = cell.Value;
                        }
                    }

                    xlWorkBook.SaveAs("cashvoucher.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                    xlWorkBook.Close(true, misValue, misValue);
                    xlApp.Quit();

                    releaseObject(xlWorkSheet);
                    releaseObject(xlWorkBook);
                    releaseObject(xlApp);

                    msg = MessageBox.Show("Report Printed: The file is saved in the Documents folder", "Print", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                catch (Exception err5)
                {
                    MessageBox.Show("Error occured while generating Excel file "+err5.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Stop);
                }
	        }

        }

        private void payPrintDataPList_Click(object sender, EventArgs e)
        {
            DialogResult msg = MessageBox.Show("Are you sure you want to print?", "Print", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);

            if (msg == DialogResult.Yes)
            {
                try
                {
                    Excel.Application xlApp;
                    Excel.Workbook xlWorkBook;
                    Excel.Worksheet xlWorkSheet;
                    object misValue = System.Reflection.Missing.Value;

                    xlApp = new Excel.ApplicationClass();
                    xlWorkBook = xlApp.Workbooks.Add(misValue);
                    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                    int i = 0;
                    int j = 0;

                    for (i = 0; i <= payDataDisplay.RowCount - 1; i++)
                    {
                        for (j = 0; j <= payDataDisplay.ColumnCount - 1; j++)
                        {
                            DataGridViewCell cell = payDataDisplay[j, i];
                            xlWorkSheet.Cells[i + 1, j + 1] = cell.Value;
                        }
                    }

                    xlWorkBook.SaveAs("payroll.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                    xlWorkBook.Close(true, misValue, misValue);
                    xlApp.Quit();

                    releaseObject(xlWorkSheet);
                    releaseObject(xlWorkBook);
                    releaseObject(xlApp);

                    msg = MessageBox.Show("Report Printed: The file is saved in the Documents folder", "Print", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                catch (Exception err5)
                {
                    MessageBox.Show("Error occured while generating Excel file " + err5.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
        }

        private void payPrintListBIR_Click(object sender, EventArgs e)
        {
            DialogResult msg = MessageBox.Show("Are you sure you want to print?", "Print", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);

            if (msg == DialogResult.Yes)
            {
                try
                {
                    Excel.Application xlApp;
                    Excel.Workbook xlWorkBook;
                    Excel.Worksheet xlWorkSheet;
                    object misValue = System.Reflection.Missing.Value;

                    xlApp = new Excel.ApplicationClass();
                    xlWorkBook = xlApp.Workbooks.Add(misValue);
                    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                    int i = 0;
                    int j = 0;

                    for (i = 0; i <= payDataDisplay.RowCount - 1; i++)
                    {
                        for (j = 0; j <= payDataDisplay.ColumnCount - 1; j++)
                        {
                            DataGridViewCell cell = payDataDisplay[j, i];
                            xlWorkSheet.Cells[i + 1, j + 1] = cell.Value;
                        }
                    }

                    xlWorkBook.SaveAs("bir.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                    xlWorkBook.Close(true, misValue, misValue);
                    xlApp.Quit();

                    releaseObject(xlWorkSheet);
                    releaseObject(xlWorkBook);
                    releaseObject(xlApp);
                    msg = MessageBox.Show("Report Printed: The file is saved in the Documents folder", "Print", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                catch (Exception err5)
                {
                    MessageBox.Show("Error occured while generating Excel file " + err5.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
        }

     

        private void assAddBatchNo_KeyPress(object sender, KeyPressEventArgs e)
        {
        


        }
   
        private void assAddBatchNo_TextChanged(object sender, EventArgs e)
        {
           
        }

        private void payAddopt_Click(object sender, EventArgs e)
        {
            panelPayAdd.Visible = true;
            panelPayModify.Visible = false;
        }

        private void payModifyopt_Click(object sender, EventArgs e)
        {
            List<string> a = new List<string>();
            a = assm.getRates();
            panelPayModify.Visible = true;
            panelPayAdd.Visible = false;
            payRates.Items.Clear();

            for (int i = 0; i < a.Count; i++)
            {
                payRates.Items.Add(a[i]);
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            bool rateCheck = false;
            int payment = 0;
            if (payRateAdd.Text != "")
            {
                try
                {
                    payment = Convert.ToInt32(payRateAdd.Text);
                    rateCheck = assm.typeCheck(payment);

                    if (rateCheck)
                    {
                        assm.addRate(payment);
                        payRateAdd.Text = "";
                        MessageBox.Show("Payment Rate and type successfully added", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        payRateAdd.Text = "";
                        MessageBox.Show("This payment rate already belongs to a specific type", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    }
                }
                catch (Exception err1)
                {
                    payRateAdd.Text = "";
                    MessageBox.Show("Please enter a valid value, only numerical values are allowed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
            else
            {
                MessageBox.Show("Please enter a valid payment rate", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void button4_Click_1(object sender, EventArgs e)
        {
            List<string> a = new List<string>();
            string s = payRates.SelectedItem.ToString();
            string[] splitter = s.Split('-');
            int type;
            int payment;
            bool ratecheck = false;

            if (payRateModify.Text != "")
            {
                try
                {
                    type = Convert.ToInt32(splitter[0]);
                    payment = Convert.ToInt32(payRateModify.Text);

                    ratecheck = assm.typeCheck(payment);

                    if (ratecheck)
                    {
                        assm.modRate(payment, type);
                        payRateModify.Text = "";
                        MessageBox.Show("Payment Rate and type successfully updated", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        payRates.Items.Clear();
                        a = assm.getRates();
                        for (int i = 0; i < a.Count; i++)
                        {
                            payRates.Items.Add(a[i]);
                        }
                    }
                    else
                    {
                        payRateModify.Text = "";
                        MessageBox.Show("This payment rate already belongs to a specific type", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    }
                }
                catch (Exception err3)
                {
                    payRateModify.Text = "";
                    MessageBox.Show("Please enter a valid value, only numerical values are allowed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
            else
            {
                MessageBox.Show("Please enter a valid payment rate", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }
    }
}

